<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="关键字">
          <el-input
            v-model="queryParams.keywords"
            placeholder="关键字"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item class="search-buttons">
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="hover" class="data-table">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableList"
        highlight-current-row
        border
        class="data-table__content"
      >
        <el-table-column label="合同名称" prop="contractName" min-width="200" />
        <el-table-column label="设计状态" prop="status" width="150">
          <template #default="scope">
            <el-tag v-if="scope.row.status" type="success">
              {{ ContractDesign_Status_Label[scope.row.status] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="设计师" prop="designerName" />
        <el-table-column label="派单时间" prop="dispatchTime" />
        <el-table-column fixed="right" label="操作" width="220">
          <template #default="scope">
            <el-button
              v-if="scope.row.status != ContractDesignStatus.CONFIRM"
              @click="showUserWindow(scope.row)"
              type="primary"
              size="small"
              link
              icon="edit"
            >
              派单
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="fetchData"
      />
    </el-card>
    <el-dialog
      v-model="dialog.visible"
      v-loading="dialog.loading"
      :title="dialog.title"
      width="80%"
      @close="closeUserWindow"
    >
      <UserSelector ref="chooseRef" :is-single="true" />
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="chooseUserDone">确 定</el-button>
          <el-button @click="closeUserWindow">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import ContractDesignAPI, {
  ContractDesignPageVO,
  ContractDesignPageQuery,
  ContractDesign_Status_Label,
  ContractDesignStatus,
} from "@/api/flow/contractDesign.api";
import UserSelector from "@/views/system/user/components/UserSelector.vue";
import { UserPageVO } from "@/api/system/user.api";

const chooseRef = ref();
defineOptions({
  name: "DesignDispatch",
  inheritAttrs: false,
});

const queryFormRef = ref();

const loading = ref(false);
const total = ref(0);
const curContractDesign = ref<ContractDesignPageVO>();
const queryParams = reactive<ContractDesignPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

// 表格数据
const tableList = ref<ContractDesignPageVO[]>();

// 弹窗
const dialog = reactive({
  title: "选择人员",
  visible: false,
  loading: false,
});

// 查询（重置页码后获取数据）
function handleQuery() {
  queryParams.pageNum = 1;
  fetchData();
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  queryParams.designer = "";
  fetchData();
}

// 获取数据
function fetchData() {
  loading.value = true;
  ContractDesignAPI.getPage(queryParams)
    .then((data) => {
      tableList.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/**
 * 关闭选人窗
 */
function closeUserWindow() {
  dialog.visible = false;
  curContractDesign.value = undefined;
}
/**
 * 打开选人窗
 */
function showUserWindow(row: ContractDesignPageVO) {
  dialog.visible = true;
  curContractDesign.value = row;
}

/**
 * 完成选人
 */
async function chooseUserDone() {
  try {
    let curList: UserPageVO = chooseRef.value.getChecked();
    if (!curList) {
      ElMessage.error("未选择用户");
      return;
    }
    await ContractDesignAPI.dispatch(curContractDesign.value?.contractId || "", curList.id);
  } catch (error) {
    console.error("添加失败", error);
  } finally {
    handleResetQuery();
    closeUserWindow();
  }
}

onMounted(() => {
  handleQuery();
});
</script>
